/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TExePos;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TExePosDao;
import com.ruipengkj.service.TExePosService;
/**
 * 说明：
 * 实施场所名称service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TExePosServiceImpl implements TExePosService{
	@Resource
	private TExePosDao TExePosDao;//DAO层
	
	
	//查询所有
	@Override
	public List<TExePos> findService(TExePos entity) throws Exception {
		String hql = "select o from TExePos o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TExePosDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据ID查询场所名称
	@Override
	public TExePos findidService(String id) throws Exception {
		String hql = "select new TExePos(o.posID,o.name) from TExePos o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (id!= null && !id.equals("")) {
			whereHQL.append("o.posID=?");
			param.add(id);
		}		
		List<TExePos> list=TExePosDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
			return list.get(0);
		}
		return null;
	}
	
	//根据ID翻页查询
	@Override
	public PageModel<TExePos> getScrollService(int firstResult,
			int maxResult, TExePos entity) throws Exception {
		String hql = "select o from TExePos o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTExePosCatalog()!=null&&entity.getTExePosCatalog().getPosCatalogID()!=null&&!entity.getTExePosCatalog().getPosCatalogID().equals("")){
				whereHQL.append("o.TExePosCatalog=?").append(BaseQuery.AND);
				param.add(entity.getTExePosCatalog());
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getShortID()!=null&&!entity.getShortID().equals("")){
				whereHQL.append("shortID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getShortID()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TExePosDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TExePos entity) throws Exception {
		TExePosDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TExePos entity) throws Exception {
		TExePosDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TExePosDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TExePos getByIdService(String id) throws Exception {
		return TExePosDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TExePos entity) throws Exception {
		TExePosDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TExePos entity) throws Exception {
		return TExePosDao.saveRetDao(entity);
	}
	public TExePosDao getTExePosDao() {
		return TExePosDao;
	}

	public void setTExePosDao(TExePosDao TExePosDao) {
		this.TExePosDao = TExePosDao;
	}
	
	@Override
	public String getTExePosShortID() throws Exception {
		String hql = "select new TExePos(o.posID, o.name, o.shortID) from TExePos o";
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("shortID", BaseQuery.DESC);
		PageModel<TExePos> pageModel=TExePosDao.getScrollDao(hql, null, null, 0, 1, orderby);
		if(pageModel!=null&&pageModel.getResultList()!=null&&pageModel.getResultList().size()!=0){
			TExePos emp=pageModel.getResultList().get(0);
			if(emp.getShortID()!=null&&!emp.getShortID().equals("")){
				return Integer.parseInt(emp.getShortID())+1+"";
			}
		}
		return "10000001";
	}
	
	@Override
	public List<TExePos> findByNameService(String name) throws Exception {
		String hql = "select new TExePos(o.posID, o.name) from TExePos o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (name != null && !name.equals("")) {
			whereHQL.append("o.name=?");
			param.add(name);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TExePosDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}
